package com.anjubao.parking.municipal.common.config;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import com.anjubao.parking.municipal.common.global.CustomConverterFactory;
import com.anjubao.parking.municipal.common.utils.U;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
public class GlobalConfig implements CommandLineRunner {
	private @Autowired RequestMappingHandlerMapping mapping;

	@Override
	public void run(String... args) throws Exception {
//		RequestMappingHandlerMapping mapping = AppContext.getBean(RequestMappingHandlerMapping.class);
		if (log.isDebugEnabled()) {
			mapping.getHandlerMethods().forEach((k, v) -> log.debug(k + " -> " + v.toString()));
		}
	}

	@Bean
	public ObjectMapper customJackson2HttpMessageConverter() {
		return U.objectMapper;
	}

	/**
	 * customConverterFactory: 绑定枚举类型参数
	 */
	@Bean
	public CustomConverterFactory customConverterFactory(FormatterRegistry registry) {
		CustomConverterFactory customConverterFactory = new CustomConverterFactory();
		registry.addConverterFactory(customConverterFactory);
		return customConverterFactory;
	}
}
